<?php
// +----------------------------------------------------------------------
// | ZanPianCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpian.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.feifeicms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use \com\SimpleDict;
class Ciku extends Model{
    function ciku_replace($find,$replace,$content,$pe=100){
		if($pe==100){
			return str_replace($find, $replace,$content);
			
		}else{
            for($x=0; $x<=count($find); $x++) {
				if(strpos($content,$find[$x])!==false){
					$s_find[$x]=$find[$x];
					$s_replace[$x]=$replace[$x];
				}
            }
            if(count($s_find)>1){
				$pin=$pe/100;
				$su=ceil(count($s_find)*$pin);
				$s_find=array_slice($s_find,0,$su);
				$s_replace=array_slice($s_replace,0,$su);
				return str_replace($s_find,$s_replace,$content);
			}			
		}
    }		
    function ciku_order_array($value,$order=0){
		if(empty($value)){
			return false;
		}
		if($order===0){
			return $newarray;
		}
		$array = preg_split('/[,;\r\n]+/', trim(nb($value), ",;\r\n"));
		foreach ($array as $key=>$val) {
			$list = explode(':', $val);
			$strlen=strlen($list[0]);
			$newarray[$key]['strlen']=$strlen;
			$newarray[$key]['val']=$val;
		}
		if($order==1){
		    array_multisort(array_column($newarray,'strlen'),SORT_DESC,$newarray);
		}elseif($order==2){
			array_multisort(array_column($newarray,'strlen'),SORT_ASC,$newarray);
		}
		$newarray=array_column($newarray,'val');
		return implode("\r\n",$newarray);
    }	
    function ciku_array($value){
		if(empty($value)){
			return false;
		}
		$value=array_unique(array_filter(parse('textarea',$value)));
		$value=arr('textarea',$value);
		$array = preg_split('/[,;\r\n]+/', trim(nb($value), ",;\r\n"));
		if(strpos($value,':')){
			$value  = array();
			foreach ($array as $key=>$val) {
				$list = explode(':', $val);
				if(isset($list[1])){
				    $value['find'][$key]= $list[0];
				    $value['replace'][$key]= $list[1];
				}else{
					continue;
				}
			}
		}else{
			$value =  $array;
	   }
       return $value;
    }
    function ciku_txt($array){
		if(empty($array)){
			return false;
		}
		if(is_array($array) && !empty($array['find']) && !empty($array['replace'])){
			foreach ($array['find'] as $key=> $val) {
				$data[$key]  = $val.':'.$array['replace'][$key];				
			}
			$array=implode("\r\n",$data);
			return $array;
		}else{
			return false;
		}
    }		
	public function datas(){
		$dir = RUNTIME_PATH.'ciku/';
        if($dh = @opendir($dir)){
			    $i=0;
            while(($file = readdir($dh)) !== false){
                if($file != '.' && $file != '..'){ 			
                        $list[$i]['name'] = pathinfo($file, PATHINFO_FILENAME);
						$data=F($list[$i]['name'],'','ciku');
                        $list[$i]['title'] = $data['title'];						
						$list[$i]['count'] = count($data['find']);
						$list[$i]['size'] = filesize($dir.$file);
						$list[$i]['addtime'] = filectime($dir.$file);
						$list[$i]['uptime'] = filemtime($dir.$file);
                }
				$i++;
            }
            closedir($dh);
        }
		if($list){
		    array_multisort(array_column($list,'uptime'),SORT_DESC,$list);
			$list=array_merge($list);
			return $list;
		}		

	}
	public function mark(){
 		$star=db('star')->order('star_hits desc')->column('star_name');	
        foreach($star as $key=>$value){
			$var[$key]=$this->myTrim($value);
			$starlist[$key]=$var[$key]."\t".$var[$key];
		}
		// $vod=db('vod')->order('vod_addtime desc')->column('vod_name');	
        // foreach($vod as $key=>$value){
			// $var[$key]=$this->myTrim($value);
			// $vodlist[$key]=$var[$key]."\t".$var[$key];
		// }
        zanpian_write_file(RUNTIME_PATH.'keywords'. DS.'keywords.txt',implode(Chr(13).chr(10),$starlist)); 
		SimpleDict::make(RUNTIME_PATH.'keywords'. DS.'keywords.txt', RUNTIME_PATH.'keywords'. DS.'keywordss.txt');
	}
    public function keywords($content){
        if(empty($content)){
		   return false;
	    }
        $dict = new SimpleDict(RUNTIME_PATH.'keywords'. DS.'keywordss.txt');
        $result=$dict->search($content);
        if($result){
			return array_keys($result);
		}else{
			return [];
		}
	}
    public function myTrim($str){
        $search = array(" ","　","\n","\r","\t");
        $replace = array("","","","","");
        return str_replace($search, $replace, $str);
    }	
}